<?php
$this->load->model('sGlobal_model');
?>

<script type="text/javascript" src="<?php echo base_url(); ?>static/select2/select2.min.js" ></script>
<script type="text/javascript" src="<?php echo base_url(); ?>static/js/selectSearchGlobal.js"></script>
<!--
<link type="text/css" href="<?php echo base_url(); ?>ajaxupload/ajaxfileupload.css" rel="stylesheet" /> 
<script src="<?php echo base_url(); ?>ajaxupload/jquery.js" type="text/javascript"></script>
<script src="<?php echo base_url(); ?>ajaxupload/ajaxfileupload.js" type="text/javascript"></script>
-->



<script>
	$(function() {
		$("input[id^='datepicker']").datetimepicker({
			scrollInput: false,
			yearOffset:0,
			lang:'en',
			timepicker:false,
			datepicker:true,
			format:'d/m/Y',
			formatDate:'d/m/Y'
		});
	});
</script>
<script>
    function cancel() {
        $.post("<?= site_url() ?>search_global", function(data) {
            window.location = '<?php echo base_url(); ?>search_global/search_global';
        });
    }
    function addServOrderSave() {
        var desc=$('#pDescription').val();
            var nilai3=desc.trim();
        var otherName=$('#pOtherName').val();
            var nilai4=otherName.trim();    
        var arrItemCek=[""];     
        var arrItemId=[""];
        var arrItemName=[""];
        var arrItemQty=[""];
        $pJumlahI=$('#jumlahItem').val();
        var x=0;
        for($x=0;$x<$pJumlahI;$x++){
            if(document.getElementById('pCek'+$x).checked==true){
                arrItemCek[$x]="Y";
            }
            else{
                arrItemCek[$x]="N";
            }
            
            arrItemId[$x]=document.getElementById("pWareItemId"+$x).value;
            arrItemName[$x]=document.getElementById('pWareItemName'+$x).value;
            arrItemQty[$x]=document.getElementById('pQtyReserv'+$x).value;
        }
        $.post("<?= site_url() ?>services/add_servOrderSave", {
            //pCType			    :  $('#pCType').val(),
            //pOrderId    		:  $('#pCtrlID').val(),
        	pHrId   			:  $('#r_id').val(),
        	pDesc    			:  nilai3,
        	pTglAwal    		:  $('#datepicker1').val(),
        	pTglAkhir    		:  $('#datepicker2').val(),
        	//pOrderDate  		:  trunc(sysdate),
        	pOrderFor    		:  $('#r_mgrid').val(),
        	pAtasanId    	    :  $('#r_mgrid').val(),
        	pAtasanApp   		:  "Y",
        	//pAtasanAppDate    :  sysdate
            pJumlahItem    	    :  $('#jumlahItem').val(),
            pItemCek            :  arrItemCek,
            pItemId             :  arrItemId,
            pItemName           :  arrItemName,
            pItemQty            :  arrItemQty,
            pOtherId			:  $('#pOtherId').val(),
            pOtherName			:  nilai4,
        }//,
        //function(data) {
        //    window.location = '<?php echo base_url(); ?>services/?';
        //}
        );
    }
</script>


<?php 
	foreach($getDetail as $a){   
	    $order_id  	                 = $a["ORDER_ID"];
		$hr_id  			         = $a["HR_ID"];
		$order_description  		 = $a["ORDER_DESCRIPTION"];
		$order_tgl_awal  	         = $a["ORDER_TGL_AWAL"];
		$order_tgl_akhir 		     = $a["ORDER_TGL_AKHIR"];
		$order_date 		         = $a["ORDER_DATE"];
		$order_for                   = $a["ORDER_FOR"];
		$order_atasan_id  			 = $a["ORDER_ATASAN_ID"];
		$order_atasan_app  		     = $a["ORDER_ATASAN_APP"];	
		$order_atasan_app_date  	 = $a["ORDER_ATASAN_APP_DATE"];
		
		$cName				= $a["NIKNAME"];
		$cJob				= $a["NIKJOBPOSITION"];
		$cOrg				= $a["NIKORG"];
		$cNameAtasan		= $a["ATASANNAME"];
 	}
    //echo $hr_id;
    /*
	if(empty($org)){
		   $org = dbora_getorg("$nik");
	}*/
?>

<div class="container">
<div class="col-md-offset-1" id="top">
    
    
<form id="formarea" role="form" method="post" action="" class="form-horizontal"  enctype="multipart/form-data">
    <input type="hidden" value="<?php echo $sesNik;?>" id="pNik" name="pNik"/>
    
    
    <div class="col-md-11">
			<div class="panel panel-default">
				<div class="panel-heading">
					<h4 class="panel-title"><b class="caret"></b><b> Request By</b></h4>
				</div>
				<div class="panel-body">
					<div class="col-xs-12 col-sm-6 col-md-6">
						<div class="form-group">
							<label class="control-label col-md-3">Name</span></label>
							<div class="col-md-7">
                            <input type="hidden" value="<?php echo $cName;?>" id="r_nama" class="select2 form-control input-sm" readonly=""/>
                            <input type="hidden" value="<?php echo $order_atasan_id;?>" id="r_mgrid" class="select2 form-control input-sm" readonly=""/>
                            <input type="hidden" value="<?php echo $cNameAtasan;?>" id="r_mgr" class="select2 form-control input-sm" readonly=""/>
                            
                            <input type="text" value="" id="r_name" class="select2 form-control input-sm" />
                            </div>
						</div>
						<div class="form-group">
							<label class="control-label col-md-3">ID Number</label>
							<div class="col-md-4">
							<input type="text" id="r_id" class="form-control input-sm" readonly=""/> 
							<!-- Edit by Andi Siswanto ~ before id="r_nik" -->
							</div> 
						</div>
					</div>
					<div class="col-xs-6 col-sm-6 col-md-6">
						<div class="form-group">
							<label class="control-label col-md-4">Departement</label>
							<div class="col-md-7">
							<input type="text" id="r_dept" class="form-control input-sm" readonly=""/>
							<!-- Edit by Andi Siswanto ~ before id="r_department" -->
							</div>
						</div>
						<div class="form-group">
							<label class="control-label col-md-4">Job Position</label>
							<div class="col-md-8">
							<input type="text" id="r_job" class="form-control input-sm" readonly=""/>
							</div>
						</div>
					</div>
				</div>
			</div>
            
            
			<div class="panel panel-default">
				<div class="panel-heading"><h4 class="panel-title"><b class="caret"></b><b> Form</b></h4></div>
				<div class="panel-body">
					<div class="col-sm-6 col-md-6">
						<div class="form-group">
							<label class="col-md-4">Tanggal</label>
							<div class="col-sm-6 form-group ">
									<div class="input-group">
										<span class="input-group-addon">From</span>
										<input type="text" class="form-control text-center input-sm" id="datepicker1"/>
									</div>
								
							</div>  
						</div>
                        <div class="form-group">
							<label class="col-md-4"></label>
							<div class="col-sm-6 form-group ">
									<div class="input-group">
										<span class="input-group-addon">To</span>
										<input type="text" class="form-control text-center input-sm" id="datepicker2"/>
									</div>
							</div>   
						</div>
					</div>
                    
					<div class="col-sm-6 col-md-6">
						<div class="form-group">
							<label class="col-md-4">Penggunaan Untuk</span></label>
							<div class="col-md-8">
								<textarea id="pDescription" class="form-control input-sm" style="min-width:190px;max-width:300px;min-height:100px;max-height:200px;">
                                </textarea>
							</div>
						</div>
					</div>
                    
                    <div class="row"> 
                        <div class="col-sm-12">
                            <div class="form-group">
                                <label class="control-label col-md-3"></label>
    				        </div>
                        </div>                   
                    </div>
                    <div class="table-responsive">
                                <table class="table table-bordered table-condensed table-striped" style="background:#f9f9f9;">
                                    <thead>
                                        <tr>
                                            <th class="tblhead" style="width:5%;text-align:center;">Cek</th>
                                            <th class="tblhead" style="width:20%;text-align:center;">Item Name</th>
                                            <th class="tblhead" style="width:10%;text-align:center;">Total Qty Reservation</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $i=0;
										$no=1;
                                        foreach ($getDetailItem as $a) {
                                                $aWareItemId   	 = $a["WAREHOUSE_ID"];
												$aWareItemName   = $a["WAREHOUSE_ITEM_NAME"];
												$aWareItemQty    = $a['WAREHOUSE_ITEM_QTY'];
												$aWareItemQtyIn  = $a['WAREHOUSE_ITEM_QTY_IN'];
												$aWareItemQtyOut = $a['WAREHOUSE_ITEM_QTY_OUT'];
                                                
                                                $getDetail3=$this->sGlobal_model->getDetailEditOrder3($cRequestNo,$aWareItemId);
                                                foreach($getDetail3 as $a){
                                                    $cnt = $a['CNT'];
                                                    $sum = $a['ORDER_DET_QTY']; 
                                                }
                                            ?>
                                        <tr>
                                            <td style="font-size:10px;">
                                                <input class="form-control input-sm" type="checkbox" id="pCek<?php echo $i; ?>" <?php if($cnt==1){?>checked=""<?php } ?>/>
                                            </td>
                                            <td style="font-size:10px;">
                                                <?php echo $aWareItemName;?>
                                            </td>
                                            <td style="font-size:10px;">
                                                <input class="form-control input-sm" type="text" id="pQtyReserv<?php echo $i; ?>" value="<?php echo $sum;?>"/>
                                            </td>
                                            <input type="hidden" id="pWareItemId<?php echo $i; ?>" value="<?php echo $aWareItemId; ?>"/>
											<input type="hidden" id="pWareItemName<?php echo $i; ?>" value="<?php echo $aWareItemName; ?>"/>
                                        </tr>
                                        <?php
                                        $i++;
                                        $no++;
                                        }
                                        ?>
                                        
                                        <input type="hidden" id="jumlahItem" value="<?php echo $i;?>"/>
                                    </tbody>
                                </table>
                </div>
                    
                    
                    <div class="col-sm-6 col-md-6">
						<div class="form-group">
							<label class="col-md-3">Other</span></label>
							<div class="col-md-8">
                                <input type="hidden" id="pOtherId"/>
								<textarea id="pOtherName" class="form-control input-sm" style="min-width:190px;max-width:600px;min-height:100px;max-height:200px;">
                                </textarea>
							</div>
						</div>
					</div>
                    
					<div class="row"> 
                        <div class="col-sm-12">
                            <div class="form-group">
                                <label class="control-label col-md-3"></label>
        						<div class="col-sm-2">
                                    <button type="button" class="btn btn-primary btn-block" onclick="addServOrderSave();">Submit</button>
                                </div>
                                <div class="col-sm-2">
                                    <button type="button" class="btn btn-default btn-block" onclick="cancel();" >Cancel</button>
                                </div>
    				        </div>
                        </div>                   
                    </div>
				</div>
			</div>
		</div>

</form>
</div>
</div>

<script type="text/javascript">
    $.validator.setDefaults({
        submitHandler: function() {
            $.post("<?= site_url() ?>public_room/insert_dmr", {
                r_id: $('#r_id').val(), 
                r_mgr: $('#r_mgr').val(), 
                courier: $('#courier').val(), 
				category: $('#category').val(), 
				cargo_type: $('#cargo_type').val(), 
				location: $('#lokasi_jemput').val(), 
				timed: $('waktu_jemput').val(), 
				D_Area: $('D_Area').val(), 
				D_Regional: $('D_Regional').val(), 
				lantai: $('#lantai').val(), 
				weight: $('#weight').val(), 
				deskripsi: $('#deskripsi').val() 
            },
            function(data) {
                $('#loader').html(data);
            });
        }
    });

    $(document).ready(function() {
        $("#formuser").keypress(function(e) {
            if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                $.validator.setDefaults({
                    submitHandler: function() {
                        $.post("<?= site_url() ?>public_room/insert_dmr", {
                            r_id: $('#r_id').val(), 
							r_mgr: $('#r_mgr').val(), 
                			courier: $('#courier').val(), 
							category: $('#category').val(), 
							cargo_type: $('#cargo_type').val(), 
							location: $('#lokasi_jemput').val(), 
							timed: $('waktu_jemput').val(), 
							D_Area: $('D_Area').val(), 
							D_Regional: $('D_Regional').val(), 
							lantai: $('#lantai').val(), 
							weight: $('#weight').val(), 
							deskripsi: $('#deskripsi').val() 
                        },
                        function(data) {
                            $('#loader').html(data);
                        });
                    }
                });
            }
        });

        $("#formuser").validate();
    });

	function getArea()
	{
		var courier_id = {courier_id:$("#courier").val()};
		$.ajax({
				type: "POST",
				url : "<?php echo site_url('public_room/ShowArea');?>",
				data: courier_id,
				success: function(msg){$('#Area').html(msg);}
			});
	};
	
	function getReg(area_name)
	{
			var getarea = {area_name:area_name.value};
			$.ajax({
				type: "POST",
				url : "<?php echo site_url('public_room/ShowRegional');?>",
				data: getarea,
				dataType:'json',
				success: function(data)
				{
					$('#D_Regional').select2({
						id: function(bond){ 
						  //return bond.AREA_ID; //value yang ada di select
                          return bond.REGIONAL_NAME;
                        },
					ajax:({	
						type:"POST",
						dataType:'json',
						url: "<?php echo site_url('public_room/getRegional')?>",
						data: function(term)
						{
							return {
										SearchReg:data,
										term:term
							};
						},
						results: function(bond, page) {return {results: bond.Show_Region, more: (bond.Show_Region && bond.Show_Region.length == 10 ? true: false)}}
						}),
						formatResult: function(bond) {return '<div class="select2-user-result">' + bond.REGIONAL_NAME + '</div>'},
						formatSelection: function(bond){return bond.REGIONAL_NAME},
						initSelection: function(elem, cb){return elem}
					});
				}
			});
	};

</script>

<script>
$("#r_name").change(function()
{
    //alert(this.id.substr(2));    
	var nik = $(this).val();
	var DataString = {id:nik};
	$.ajax({
			type: "POST",
			url : "<?php echo site_url('search_global/globals_nik_detail')?>",
			dataType: 'json',
			data: DataString,
			success: function(data)
			{
				$("#r_id").val(data.DetailEmp.HR_ID);
                $("#r_nama").val(data.DetailEmp.NAME);
                $("#r_mgrid").val(data.DetailEmp.MANAGER_ID);
				$("#r_mgr").val(data.DetailEmp.MANAGER);
				$("#r_dept").val(data.DetailEmp.ORG);
				$("#r_job").val(data.DetailEmp.JOB_POSITION);														
			}
		});
});
</script>